package com.eascs.ticket.project.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.exception.ServiceException;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.project.dao.ProjectVersionDao;
import com.eascs.ticket.project.entity.ProjectVersion;
import com.eascs.ticket.project.entity.ProjectVersionUpdate;

@Service
public class ProjectVersionService extends BaseSevice {
	@Inject
	private ProjectVersionDao projectVersionDao;

	public List<Object[]> getValidProjectVersionByProjectId(String projectId) {
		return this.projectVersionDao.getValidProjectVersionByProjectId(projectId);
	}

	public List<Object[]> getProjectVersionByProjectId(String projectId) {
		return this.projectVersionDao.getProjectVersionByProjectId(projectId);
	}

	public List<Object[]> getProjectVersionTableViewByProjectId(String projectId) {
		return this.projectVersionDao.getProjectVersionTableViewByProjectId(projectId);
	}

	public ProjectVersion getProjectVersionById(String projectVersionId) {
		return this.proxyDao.getById(ProjectVersion.class, projectVersionId);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertProjectVersion(ProjectVersion projectVersion) {
		EntityUtil.initInsertEntity(projectVersion);
		this.proxyDao.insert(projectVersion);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void updateProjectVersion(ProjectVersionUpdate projectVersionUpdate) {
		EntityUtil.initUpdateEntity(projectVersionUpdate);
		this.proxyDao.updateById(projectVersionUpdate);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteProjectVersion(String projectVersionId) {
		ProjectVersion projectVersion = this.proxyDao.getById(ProjectVersion.class, projectVersionId);
		if (this.projectVersionDao.countProjectVersionInTicket(projectVersion.getProjectId(), projectVersion.getId()) > 0) {
			throw new ServiceException("该版本已经被引用，不能执行删除！");
		}
		this.proxyDao.deleteByIdWithEntity(projectVersion);
	}
}
